package org.datastealer.dao.oracle;

import lombok.SneakyThrows;
import org.datastealer.bean.DataSource;
import org.datastealer.dao.api.DashBoardDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DatabaseMetaData;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：oracle仪表盘DAO层接口
 * @date ：Created in 2020/9/28
 */
@Component("dashBoardDao")
public class OracleDashBoardDao implements DashBoardDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    @SneakyThrows
    public DataSource getDataSource() {
        Connection connection = jdbcTemplate.getDataSource().getConnection();
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        DataSource dataSource = DataSource.builder().url(databaseMetaData.getURL())
                .userName(databaseMetaData.getUserName())
                .databaseProductName(databaseMetaData.getDatabaseProductName())
                .databaseProductVersion(databaseMetaData.getDatabaseProductVersion())
                .driverName(databaseMetaData.getDriverName())
                .driverVersion(databaseMetaData.getDriverVersion())
                .dataSourceClassName(jdbcTemplate.getDataSource().getClass().getName())
                .autoCommit(connection.getAutoCommit())
                .build();
        // 关闭连接，防止泄露
        connection.close();
        return dataSource;
    }

}